/*
* PureMVC Logger v1.0
* Copyright (c) 2009 We Are Cloud.
* Author: Yannick Chaze
*
* Based on PureMVC code
* See http://puremvc.org for more informations
*
* Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0.
*
* PureMVC MultiCore - Copyright(c) 2006-08 Futurescale, Inc., Some rights reserved.
* Your reuse is governed by the Creative Commons Attribution 3.0 United States License
*/

package com.wearecloud.puremvclogger.patterns.facade
{
	import com.wearecloud.puremvclogger.Logger;
	import com.wearecloud.puremvclogger.core.ControllerLogger;
	import com.wearecloud.puremvclogger.core.ModelLogger;
	import com.wearecloud.puremvclogger.core.ViewLogger;
	
	import org.puremvc.as3.multicore.interfaces.IFacade;
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.IProxy;
	import org.puremvc.as3.multicore.patterns.facade.Facade;

	/**
	 * Facade extends the original facade and just log 
	 * before call methods of the parent class.
	 */ 
	public class FacadeLogger extends Facade implements IFacade
	{
		/**
		 * @inheritDoc
		 */
		public function FacadeLogger( key:String = null )
		{
			Logger.log ( "Instantiate facade");
			super( key );
		}
		
		/**
		 * @inheritDoc
		 */
		public static function getInstance( key:String = null ):IFacade {
			if (instanceMap[ key ] == null ) instanceMap[ key ] = new FacadeLogger( key );
			return instanceMap[ key ];
		}
		
		/**
		 * @inheritDoc
		 */
		override protected function initializeModel() : void
		{
			if ( model != null) return;
			model = ModelLogger.getInstance( multitonKey );
		}
		
		/**
		 * @inheritDoc
		 */
		override protected function initializeView() : void
		{
			if ( view != null) return;
			view = ViewLogger.getInstance( multitonKey );
		}
		
		/**
		 * @inheritDoc
		 */
		override protected function initializeController() : void
		{
			if ( controller != null) return;
			controller = ControllerLogger.getInstance( multitonKey );
		} 
		
		/**
		 * @inheritDoc
		 */
		override public function registerProxy(proxy:IProxy):void
		{
			Logger.log( "Regitrering proxy: " + proxy.getProxyName());
			super.registerProxy( proxy );
		}
		
		/**
		 * @inheritDoc
		 */
		override public function retrieveProxy(proxyName:String):IProxy
		{
			Logger.log("Retrieving proxy: " + proxyName);
			return super.retrieveProxy( proxyName );
		}
		
		/**
		 * @inheritDoc
		 */
		override public function sendNotification(notificationName:String, body:Object=null, type:String=null):void
		{
			Logger.log( "Sending notification: " + notificationName );
			super.sendNotification( notificationName, body, type );
		}
		
		/**
		 * @inheritDoc
		 */
		override public function removeProxy(proxyName:String):IProxy
		{
			Logger.log( "Removing proxy: " + proxyName );
			return super.removeProxy( proxyName );
		}
		
		/**
		 * @inheritDoc
		 */
		override public function hasProxy(proxyName:String):Boolean
		{
			return super.hasProxy( proxyName );
		}
		
		/**
		 * @inheritDoc
		 */
		override public function registerCommand(noteName:String, commandClassRef:Class):void
		{
			Logger.log( "Regitrering command: " + commandClassRef + " for notification: " +  noteName);
			super.registerCommand( noteName, commandClassRef );
		}
		
		/**
		 * @inheritDoc
		 */
		override public function removeCommand(notificationName:String):void
		{
			Logger.log( "Removing command associated to: " + notificationName );
			super.removeCommand( notificationName );
		}
		
		/**
		 * @inheritDoc
		 */
		override public function hasCommand(notificationName:String):Boolean
		{
			return super.hasCommand( notificationName );
		}
		
		/**
		 * @inheritDoc
		 */
		override public function registerMediator(mediator:IMediator):void
		{
			Logger.log( "Registering mediator: " + mediator.getMediatorName());
			super.registerMediator( mediator );
		}
		
		/**
		 * @inheritDoc
		 */
		override public function retrieveMediator(mediatorName:String):IMediator
		{
			Logger.log( "Retrieving mediator: " + mediatorName);
			return super.retrieveMediator( mediatorName );
		}
		
		/**
		 * @inheritDoc
		 */
		override public function removeMediator(mediatorName:String):IMediator
		{
			Logger.log( "Removing mediator: " + mediatorName);
			return super.removeMediator( mediatorName );
		}
		
		/**
		 * @inheritDoc
		 */
		override public function hasMediator(mediatorName:String):Boolean
		{
			return super.hasMediator( mediatorName );
		}
		
		/**
		 * @inheritDoc
		 */
		public static function removeCore( key:String ) : void
		{
			if (instanceMap[ key ] == null) return;
			ModelLogger.removeModel( key ); 
			ViewLogger.removeView( key );
			ControllerLogger.removeController( key );
			delete instanceMap[ key ];
		}
	}
}